﻿
using HandyControl.Controls;
using HandyControl.Tools.Extension;
using System;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using Uipath_License.Model;
using Uipath_License.Tools;
using Uipath_License.Uc.UCTools;

namespace Uipath_License.Uc
{
    /// <summary>
    /// UC_ManageLicense.xaml 的交互逻辑
    /// </summary>
    public partial class UC_ManageLicense : UserControl
    {
        public UC_ManageLicense()
        {
            InitializeComponent();
        }

        private void License_Message_Button_Click(object sender, RoutedEventArgs e)
        {
            Uipath_LicenseHelper uipath_LicenseHelper = new Uipath_LicenseHelper();
            HandyControl.Controls.MessageBox.Show(uipath_LicenseHelper.GetLicense_Message(), "信息", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void Auto_Net_Activate_Button_Click(object sender, RoutedEventArgs e)
        {
            ProgressButton button = sender as ProgressButton;

            button.IsChecked = true;
            button.IsEnabled = false;
            Auto_Net_Activate_TextBlock.Text = "正在自动激活";
            Thread thread = new Thread(new ThreadStart(() =>
            {
                Uipath_LicenseHelper uipath_LicenseHelper = new Uipath_LicenseHelper();
                Response_License license = uipath_LicenseHelper.NetGetLicense();
                if (license == null || license.IsError)
                {
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        HandyControl.Controls.MessageBox.Show("激活失败：\n" + license?.ErrorMessage, "错误", MessageBoxButton.OK, MessageBoxImage.Error);
                        button.IsChecked = false;
                        button.IsEnabled = true;
                        Auto_Net_Activate_TextBlock.Text = "一键激活";
                    }));
                    return;

                }

                string res = uipath_LicenseHelper.Net_Activate(license.LicenseCode);
                if (res.Contains("ActiveLicense"))
                {
                    HandyControl.Controls.MessageBox.Success("激活成功");
                    Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                    {
                        button.IsChecked = false;
                        button.IsEnabled = true;
                        Auto_Net_Activate_TextBlock.Text = "一键激活";
                    }));
                    return;
                }
                if (res.Contains("There is a license already"))
                {
                    MessageBoxResult mesbox = HandyControl.Controls.MessageBox.Show("当前本地存在License,是否继续激活？\n继续激活将停用本地存在的License！", "请确认", MessageBoxButton.YesNo, MessageBoxImage.Question);
                    if (mesbox == MessageBoxResult.No)
                    {
                        Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                        {
                            button.IsChecked = false;
                            button.IsEnabled = true;
                            Auto_Net_Activate_TextBlock.Text = "一键激活";
                        }));
                        return;

                    }
                    string deactivate_res = uipath_LicenseHelper.Net_Deactivate();
                    //if (deactivate_res.Contains("NoLicenseAvailable"))ActiveLicense
                    //{

                    //}


                }
                res = uipath_LicenseHelper.Net_Activate(license.LicenseCode);
                if (res.Contains("ActiveLicense"))
                {
                    HandyControl.Controls.MessageBox.Success("激活成功");
                }
                else
                {
                    HandyControl.Controls.MessageBox.Error("激活失败");
                }
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    button.IsChecked = false;
                    button.IsEnabled = true;
                    Auto_Net_Activate_TextBlock.Text = "一键激活";
                }));
            }));
            thread.Start();


        }

        private void Net_Deactivate_Button_Click(object sender, RoutedEventArgs e)
        {
            ProgressButton button = sender as ProgressButton;
            button.IsChecked = true;
            button.IsEnabled = false;
            Uipath_LicenseHelper uipath_LicenseHelper = new Uipath_LicenseHelper();
            Net_Deactivate_Button_TextBlock.Text = "正在停用";
            Thread thread = new Thread(() => 
            {
                string deactivate_res = uipath_LicenseHelper.Net_Deactivate();
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    button.IsChecked = false;
                    button.IsEnabled = true;
                    Net_Deactivate_Button_TextBlock.Text = "停用";
                }));
                HandyControl.Controls.MessageBox.Success("停用完成");
            });
            thread.Start();
        }

        private void Net_Update_Button_Click(object sender, RoutedEventArgs e)
        {
            ProgressButton button = sender as ProgressButton;
            button.IsChecked = true;
            button.IsEnabled = false;
            Uipath_LicenseHelper uipath_LicenseHelper = new Uipath_LicenseHelper();
            Net_Update_Button_TextBlock.Text = "正在更新";
            Thread thread = new Thread(() =>
            {
                string deactivate_res = uipath_LicenseHelper.Net_Update();
                Application.Current.Dispatcher.BeginInvoke(new Action(() =>
                {
                    button.IsChecked = false;
                    button.IsEnabled = true;
                    Net_Update_Button_TextBlock.Text = "更新";
                }));
                HandyControl.Controls.MessageBox.Success("更新完成");
            });
            thread.Start();
        }

        private void License_Net_Activate_Button_Click(object sender, RoutedEventArgs e)
        {
            UC_Window uC_Window = new UC_Window( new UC_InputNetActivate(),"License在线激活");
            uC_Window.Show();
        }

        private void License_Activate_Button_Click(object sender, RoutedEventArgs e)
        {
            UC_Window uC_Window = new UC_Window(new UC_Activation_Request(), "License离线激活");
            uC_Window.Show();
        }

        private void DisableLicense_Click(object sender, RoutedEventArgs e)
        {
            UC_Window uC_Window = new UC_Window(new UC_DisableLicense(), "License离线停用");
            uC_Window.Show();
        }
    }
}
